package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.props.PropertyStoreException;
import sk.naive.talker.message.*;
import sk.naive.talker.util.Buffer;

import java.rmi.RemoteException;
import java.util.Map;


/**
 * AddressedPrivateMessageCommand.
 *
 * @author <a href="mailto:rytier@naive.deepblue.sk">Martin "Rytier" Kerni</a>
 * @version $Revision: 1.16 $ $Date: 2005/02/07 21:20:39 $
 */
public abstract class AddressedPrivateMessageCommand extends AddressedMessageCommand {
	protected final void execSend(User recipient, String message)
		throws RemoteException,
		CommandException,
		PropertyStoreException
	{
		Map ctx = user.getProperties();
		ctx.put(DefaultMessageFactory.CTXKEY_VAL + "Rcp", recipient.getProperties());
		ctx.put(DefaultMessageFactory.CTXKEY_VAL, message);
		AfkStatus afkStatus = new AfkHelper(recipient).getStatus();

		Message storedSender;
		Message storedRecipient;

		if (afkStatus == null) {
			if (message != null) {
				storedSender = getMessage(getStoredSenderMessageKey(message), ctx);
				storedRecipient = getMessage(getStoredRecipientMessageKey(message), ctx);

				if (user != recipient) {
					sendHelper().sendMessage(user, getSenderMessageKey(message));
				}
				sendRecipientMessage(recipient, getRecipientMessageKey(message));
			} else {
				storedSender = getMessage(getStoredSenderEmptyMessageKey(message), ctx);
				storedRecipient = getMessage(getStoredRecipientEmptyMessageKey(message), ctx);

				if (user != recipient) {
					sendHelper().sendMessage(user, getSenderEmptyMessageKey(message));
				}
				sendRecipientMessage(recipient, getRecipientEmptyMessageKey(message));
			}
		} else {
			if (message != null) {
				storedSender = getMessage(getStoredSenderAfkMessageKey(message), ctx);
				storedRecipient = getMessage(getStoredRecipientAfkMessageKey(message), ctx);

				if (user != recipient) {
					sendHelper().sendMessage(user, getSenderAfkMessageKey(message));
				}
				sendRecipientMessage(recipient, getRecipientAfkMessageKey(message));
			} else {
				storedSender = getMessage(getStoredSenderAfkEmptyMessageKey(message), ctx);
				storedRecipient = getMessage(getStoredRecipientAfkEmptyMessageKey(message), ctx);

				if (user != recipient) {
					sendHelper().sendMessage(user, getSenderAfkEmptyMessageKey(message));
				}
				sendRecipientMessage(recipient, getRecipientAfkEmptyMessageKey(message));
			}
		}
		Buffer review = getBuffer(user, Revtell.UPROP_TELL_REVIEW);
		if (user != recipient) {
			review.add(storedSender);
		}

		review = getBuffer(recipient, Revtell.UPROP_TELL_REVIEW);
		review.add(storedRecipient);

		customExec(recipient, message);
	}

	/**
	 * Should be overidden if custom action after send is required.
	 *
	 * @param recipient
	 * @param message
	 * @throws RemoteException
	 * @throws CommandException
	 */
	protected void customExec(User recipient, String message) throws RemoteException, CommandException, PropertyStoreException {
	}

	protected String getStoredRecipientMessageKey(String message) {
		return getRecipientMessageKey(message);
	}

	protected String getStoredSenderMessageKey(String message) {
		return getSenderMessageKey(message);
	}

	protected String getRecipientEmptyMessageKey(String message) {
		return getRecipientMessageKey(message);
	}

	protected String getStoredRecipientEmptyMessageKey(String message) {
		return getRecipientEmptyMessageKey(message);
	}

	protected String getSenderEmptyMessageKey(String message) {
		return getSenderMessageKey(message);
	}

	protected String getStoredSenderEmptyMessageKey(String message) {
		return getSenderEmptyMessageKey(message);
	}

	protected String getStoredRecipientAfkMessageKey(String message) {
		return getStoredRecipientMessageKey(message);
	}

	protected String getStoredSenderAfkMessageKey(String message) {
		return getStoredSenderMessageKey(message);
	}

	protected String getRecipientAfkEmptyMessageKey(String message) {
		return getRecipientEmptyMessageKey(message);
	}

	protected String getStoredRecipientAfkEmptyMessageKey(String message) {
		return getStoredRecipientEmptyMessageKey(message);
	}

	protected String getSenderAfkEmptyMessageKey(String message) {
		return getSenderEmptyMessageKey(message);
	}

	protected String getStoredSenderAfkEmptyMessageKey(String message) {
		return getStoredSenderEmptyMessageKey(message);
	}

	protected String getRecipientAfkMessageKey(String message) {
		return getRecipientMessageKey(message);
	}

	protected String getSenderAfkMessageKey(String message) {
		return getSenderMessageKey(message);
	}

	protected Category getCategory() {
		return Category.TELL;
	}
}
